package com.jc.order.controller;

import com.jc.order.common.DictionaryToJcConst;
import com.jc.order.constant.OrderConst;
import com.jc.order.dto.JcPolicyDTO;
import com.jc.order.dto.SystemTenantDTO;
import com.jc.order.service.*;
import com.jc.util.Result;
import com.jc.vo.OrderRequest;
import com.jc.vo.PolicyRequest;
import com.jc.vo.PolicyResponseVO;
import com.jc.vo.PolicyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("traditional/v1")
@Slf4j
public class TraditionalOrderController extends BaseOrderController {
    @Autowired
    private InsuranceTraditionalService insuranceTraditionalService;
    @Resource(name = "insuranceTraditionalValidateService")
    private CargoValidateService cargoValidateService;
    @Autowired
    private CargoFeeCalculateService cargoFeeCalculateService;
    @Autowired
    private ApiBaseService apiBaseService;

    @Override
    protected CargoCrossBorderService<JcPolicyDTO> getInsuranceService() {
        return insuranceTraditionalService;
    }

    @Override
    protected CargoValidateService getValidateService() {
        return cargoValidateService;
    }
    @Override
    protected String getProductCode() {
        return OrderConst.PRODUCT_CODE_1;
    }

//    @Override
//    protected CargoFeeCalculateService getFeeCalculateService() {
//        return cargoFeeCalculateService;
//    }
//
//    @Override
//    protected ApiBaseService getApiBaseService() {
//        return apiBaseService;
//    }

    @Override
    protected void buildInsuranceData(PolicyRequest request, SystemTenantDTO tenant, JcPolicyDTO dto) {
        JcPolicyDTO policyDTO = apiBaseService.buildBaseInsuranceData(request, tenant);
        BeanUtils.copyProperties(policyDTO, dto);
        setBusinessFields(request, dto);
    }



    // 映射到父类方法
    @RequestMapping("createOrder")
    public Result<PolicyResponseVO> createOrder(@RequestBody OrderRequest request) {
        return super.createOrder(request);
    }

    @RequestMapping("calculatePolicy")
    public Result calculatePolicy(@RequestBody OrderRequest request) {
        return super.calculatePolicy(request);
    }


    /**
     * jc
     * @param request
     * @return
     */
    @RequestMapping("createPolicy")
    public Result createPolicy(@RequestBody OrderRequest request) {




        return super.createPolicy(request);
    }

    @RequestMapping("updatePolicy")
    public Result updatePolicy(@RequestBody OrderRequest request) {
        return super.updatePolicy(request);
    }

    @RequestMapping("queryPolicy")
    public Result<List<PolicyVO>> queryPolicy(@RequestBody OrderRequest request) {
        return super.queryPolicy(request);
    }



    // 传统保险特有字段设置
    private void setBusinessFields(PolicyRequest request, JcPolicyDTO dto) {
        if (request == null) return;

        dto.setStartTimeDisplayMode(request.getDepartureDateFlag() != null && !request.getDepartureDateFlag() ?
                new Byte("2") : new Byte("1"));
        dto.setInvoiceNumber(request.getInvoiceNo());
        dto.setContractNumber(request.getBargainNo());
        dto.setCreditNumber(request.getCreditNo());
        dto.setCreditTerms(request.getCreditLetter());

        Integer insuranceConfirmType = DictionaryToJcConst.INSURANCECONFIRMTYPE_MAP.get(request.getBaseAmountWay());
        dto.setInsuranceConfirmType(insuranceConfirmType);
        dto.setWarehouseTermsRequired(Optional.ofNullable(request.getWhsToWhs()).orElse(false));
    }
}